<?php 
/**
 *
 * 管理员查询功能
 */
class AdminSearch
{

	var $pdo = null;
    var $logger = null;
    function __construct()
    {
        $this->pdo = PDOWrapper::instance();
        $this->logger = CLog::getInstance();
    }

	/**
     * @param $GetParams
     * @param $PostArray
     * @return bool
     * 代理查询
     */
    function AgentList($PostArray) {

        $sql = "SELECT user_id,name,nickname,user_type,mobile,portrait,city FROM users WHERE user_id IN (SELECT user_id FROM agent)";
        $res = $this->pdo->query($sql);

        $data['agents'] = $res;
        return $data;
    }

    /**
     * @param $GetParams
     * @param $PostArray
     * @return bool
     * 管理员列表
     */
    function AdminList($PostArray) {

        $params = array(
            'admin_id',
            'admin_type',
            'name',
            'mobile',
            'email',
            'gender',
            'portrait',
            'city',
            'dept',
            'last_login_ip',
            'last_login_time',
            'flag',
            'created_at'
        );
        $res = $this->pdo->select('admins',$params);

        $data['admins'] = $res;

        $params = array(
            'dept_id',
            'dept_name',
            'create_time'
        );
        $dept = $this->pdo->select('department',$params);

        $data['admins'] = $res;
        $data['dept'] = $dept;
        return $data;
    }

    
    
    /**
     * @param $GetParams
     * @param $PostArray
     * @return bool
     * 用户列表
     */
    function UserList($PostArray){

        $page_no = 0;
        if(isset($PostArray['page_no'])){
            $page_no = $PostArray['page_no'];
        }
        $page_size = 10;
        if(isset($PostArray['page_size'])){
            $page_size = $PostArray['page_size'];
        }
        $start =  $page_no * $page_size;

        $sql="SELECT user_id,user_type,name,nickname,mobile,email,gender,birth,portrait,city,region,address,last_login_ip,last_login_time,created_at,reg_id,reg_terminal FROM users ";

        if(isset($PostArray['mobile'])){
            $mobile = $PostArray['mobile'];
            $sql= $sql." WHERE mobile={$mobile}";
        }

        $sql=  $sql." LIMIT {$start},{$page_size}";

        $users = $this->pdo->query($sql);
        foreach($users as $key=>$value){
            $sql="SELECT sum(num) AS coupon_num FROM coupons WHERE user_id={$value['user_id']} AND  expired_date > now()";
            $num = $this->pdo->query($sql);
            $users[$key]['coupon_num'] =$num[0]['coupon_num'];
        }

        $sql="SELECT count(user_id) AS num  FROM users ";
        $count = $this->pdo->query($sql);

        $data = array(
            'user_num'=>$count[0]['num'],
            'users'=>$users
        );

        return $data;
    }

    

    /**
     * @param $GetParams
     * @param $PostArray
     * @return bool
     * 各部门权限列表
     */
    function AuthList($PostArray){

        if(empty($PostArray['dept_id'])) {
            Utils::ShowError(ErrorDefine::REQUIRED_FIELD_EMPTY);
            return false;
        }
        $authlist = [];
        $fields = ['dept_id','dept_name'];
        $dept = $this->pdo->select('department');
        $authlist['dept'] = $dept;
        $authlist['dept_id'] = $PostArray['dept_id'];
        // foreach ($dept as $key => $value) {

        //     $sql = "SELECT `function`.func_name,auth.auth FROM `function` LEFT JOIN auth ON auth.auth_id=func_id+".$value['dept_id'];
        //     $authlist[$value['dept_id']] = $this->pdo->query($sql);
        // }
        $sql = "SELECT `function`.func_name,auth.auth FROM `function` LEFT JOIN auth ON auth.auth_id=func_id+".$PostArray['dept_id'];
        $authlist['auth'] = $this->pdo->query($sql);


        return $authlist;
    }


}